<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Myzen
 * @package    Myzen_Db
 * @subpackage Profiler
 * @copyright  Copyright (c) 2010 COPEO
 */

/** Zend_Db_Profiler */
require_once 'Zend/Db/Profiler.php';

/** Zend_Wildfire_Plugin_FirePhp */
require_once 'Zend/Wildfire/Plugin/FirePhp.php';

/** Zend_Wildfire_Plugin_FirePhp_TableMessage */
require_once 'Zend/Wildfire/Plugin/FirePhp/TableMessage.php';

/**
 * Writes DB events as log messages to the log file.
 * 
 * @category   Zend
 * @package    Zend_Db
 * @subpackage Profiler
 * @copyright  Copyright (c) 2010 COPEO
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Myzen_Db_Profiler_Log extends Zend_Db_Profiler
{
	
	
  
    /**
     * Contains the log object on which the query should be referenced
     * @var object
     */
    protected $_logger = null;
    
    /**
     * Contains the level on which the log should be written
     * @var int
     */
    protected $_level = 7;    
  
    /**
     * Constructor
     *
     * @param string $label OPTIONAL Label for the profiling info.
     * @return void
     */
    public function __construct($oLog,$level = 7)
    {
		$this->_logger = $oLog;
    }

    /**
     * Enable or disable the profiler.  If $enable is false, the profiler
     * is disabled and will not log any queries sent to it.
     *
     * @param  boolean $enable
     * @return Zend_Db_Profiler Provides a fluent interface
     */
    public function setEnabled($enable)
    {
        parent::setEnabled($enable);
        return $this;
    }

    /**
     * Intercept the query end and log the profiling data.
     *
     * @param  integer $queryId
     * @throws Zend_Db_Profiler_Exception
     * @return void
     */
    public function queryEnd($queryId)
    {
        parent::queryEnd($queryId);
        
        if (!$this->getEnabled()) {
            return;
        }

        $profile = $this->getQueryProfile($queryId);
        
		$params=($params = $profile->getQueryParams())?$params:array();
        $paramsFlat=implode('-',$params);
        $this->_logger->log('profiling:'.round($profile->getElapsedSecs(),5).'|'.$profile->getQuery().'|'.$paramsFlat,$this->_level);
                                      
    }
    

}
